package com.xyl.login.controller;

import com.xyl.common.security.SecurityUtils;
import com.xyl.user.entity.User;
import com.xyl.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Pattern;

/**
 * 注册页面
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public String register() {
        return "register";
    }

    //前端点击注册时  进行注册
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody    //这里看似返回 为json   其实不然。 返回的是字符串  然后通过前端进行对字符串比较后 再进行之后的操作
    //前端把数据封装成json格式后 在进行发送
    public String registerCheck(@RequestBody User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        if (user != null) {
            //判断该昵称是否已存在
            if(!userService.checkName(user.getName())){
                return "error1";               //用户名已存在
            }
            //判断手机验证码是否有误
            if (!check(user)) {
                return "error";
            }
            //可能对接  学生学号接口  进行比对  此学生是否存在
            String result = SecurityUtils.encryptPassword(user.getPassword());
            user.setPassword(result);
            if (userService.registerUser(user) == 1) {
                //插入数据成功
                return "succ";
            } else {
                //插入失败
                return "error_add_user";
            }
        } else {
            //注册失败   user为空
            return "error";
        }
    }

    //点击注册时检查用户信息是否合法
    private boolean check(User user) {
        return true;
    }

    //当点击获取验证码的时候  检查手机号是否合法  以及 是否已注册
    @RequestMapping(value = "/getVerification",method = RequestMethod.POST)
    public String checkPhoneNumber(@RequestBody User user,HttpServletRequest request){
        String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String phone = user.getPhone().toString();
        
        if(phone!=null){
            if(!Pattern.matches(REGEX_MOBILE,phone)){
                return "error_phone";  //手机号不合法
            }
        }
        //发送短信操作
        int result = userService.sendVercode(user.getPhone(),getIpFromRequest(request));
        if(result != 1){
            return "";   //返回异常数据
        }
        return "";      //返回成功数据
    }

    /**
     * 查找IP    请求可能经过nginx转发  为了找到实际IP地址 就有了以下操作
     * @param request
     * @return
     */
    protected String getIpFromRequest(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }

}
